package com.bwei.mypaot.base.activity;

import android.Manifest;
import android.content.pm.PackageManager;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.bwei.mypaot.R;
import com.bwei.mypaot.view.LetterView;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;

/**
 * Created by Administrator on 2017/2/15.
 */

public class CityActivity extends BaseActivity {

    private String[] city_group_list;
    private String[] cityname_list;
    private ListView listView;
    private HashMap<String, Integer> citymap;
    private LetterView letterView;
    private ImageView location;
    private LocationManager locationManager;
    private TextView cityname;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.city_activity);
        getData();
        initMap();
        initHeader();
        initWidget();
        setWidgetState();
        getLocation();

    }

    @Override
    public void initHeader() {
        inittHeaderWidget();
        setTitle("切换城市");
        addImageLeftListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

    }

    @Override
    public void initWidget() {
        letterView = (LetterView) findViewById(R.id.letterView);
        listView = (ListView) findViewById(R.id.listview);
        location = (ImageView) findViewById(R.id.location);
        cityname = (TextView) findViewById(R.id.cityname);


    }

    @Override
    public void setWidgetState() {
        location.setOnClickListener(this);
        letterView.setOnLetterSelectListener(new LetterView.OnLetterSelectListener() {
            @Override
            public void setSelectListener(String s) {
                Toast.makeText(CityActivity.this, s, Toast.LENGTH_SHORT).show();
                //从map集合中取出位置
                Integer position = citymap.get(s);
                //调用setSelection方法切换位置
                listView.setSelection(Integer.parseInt(position + ""));
            }
        });
        listView.setAdapter(new MyAdapter());
    }

    public void getData() {
        //获得城市集合
        city_group_list = CityActivity.this.getResources().getStringArray(R.array.city_group_list);
        //获得字母集合
        cityname_list = CityActivity.this.getResources().getStringArray(R.array.city_description_list);
    }

    //创建map集合
    public void initMap() {
        citymap = new HashMap<String, Integer>();
        //循环城市集合得到前一个下标的字母
        for (int i = 0; i < cityname_list.length; i++) {
            String presort = "";
            if (i - 1 >= 0) {
                presort = city_group_list[i - 1];
            }
            //如果右侧的字母不等于左侧的字母那么就把右侧的字母所对应的下标和城市所对应的字母存入集合
            if (!city_group_list[i].equals(presort)) {
               //把获得的字母下标存入集合中
                citymap.put(city_group_list[i], i);
            }

        }
    }

    class MyAdapter extends BaseAdapter {

        private String prename;

        @Override
        public int getCount() {
            return cityname_list.length;
        }

        @Override
        public Object getItem(int i) {
            return cityname_list[i];
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            if (view == null) {
                view = View.inflate(CityActivity.this, R.layout.city_item, null);
            }
            TextView tv_namesort = (TextView) view.findViewById(R.id.tv_namesort);
            TextView city_name = (TextView) view.findViewById(R.id.city_name);
            //判断如果上一个位置不为0，则获得上一个位置的字母
            if (i - 1 >= 0) {
                prename = city_group_list[i - 1];
            }
            //把上一个字母与相邻字母比较，如果相同就隐藏，否则就显示
            if (city_group_list[i].equals(prename)) {

                tv_namesort.setVisibility(View.GONE);
            } else {
                tv_namesort.setVisibility(View.VISIBLE);
            }
            //为字母和城市名控件赋值
            tv_namesort.setText(city_group_list[i]);
            city_name.setText(cityname_list[i]);

            return view;
        }
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);

    }
        //定位的方法
    public void getLocation() {
        locationManager = (LocationManager) getSystemService(CityActivity.this.LOCATION_SERVICE);

        LocationProvider netProvider = locationManager.getProvider(LocationManager.NETWORK_PROVIDER);
        //动态设置权限
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
                && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION)
                != PackageManager.PERMISSION_GRANTED) {

            Toast.makeText(CityActivity.this,"无法定位",Toast.LENGTH_LONG).show();
            return;
        }
        //的到location对象
        Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

      Log.d("cccc","wwwwwwwwwwwwww");
        //得到纬度
        double latitude = location.getLatitude();
        //得到经度
        double longitude = location.getLongitude();

        Toast.makeText(CityActivity.this,"精度"+ latitude+"--"+longitude,Toast.LENGTH_LONG).show();
        Geocoder gc = new Geocoder(this, Locale.getDefault());
        List<Address> locationList = null;
        try {
            locationList = gc.getFromLocation(latitude, longitude, 1);

        } catch (IOException e) {
            e.printStackTrace();
        }
        Address address = locationList.get(0);//得到Address实例
        String countryName = address.getCountryName();//得到国家名称，比如：中国
        String locality = address.getLocality();//得到城市名称，比如：北京市
        Toast.makeText(CityActivity.this, countryName+"--"+locality,Toast.LENGTH_LONG).show();
        //为控件设置值
        cityname.setText(locality);

    }
}
